package com.xpsoft.oa.workflow.event;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.produce.MaterialPurReal;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.produce.MaterialPurRealService;
import com.xpsoft.oa.service.produce.MaterialPurchaseService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;

public class MaterialPurRealFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(MaterialPurRealFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String userId = (String)pi.getVariable("materialPurReal.createUser.userId");
		Date createTime = (Date)pi.getVariable("materialPurReal.createTime");
		String purchaseId = (String)pi.getVariable("materialPurReal.materialPurchase.id");
		String proId = (String)pi.getVariable("materialPurReal.project.id");
		String planNo = (String)pi.getVariable("materialPurReal.planNo");
		String quality = (String)pi.getVariable("materialPurReal.quality");
		String dataList = (String)pi.getVariable("materialPurReal.dataList");
		
		MaterialPurReal materialPurReal = new MaterialPurReal();
		materialPurReal.setPlanNo(planNo);
		materialPurReal.setCreateTime(createTime);
		materialPurReal.setQuality(quality);
		
		if(StringUtils.isNotBlank(purchaseId)){
			MaterialPurchaseService materialPurchaseService = (MaterialPurchaseService)AppUtil.getBean("materialPurchaseService");
			materialPurReal.setMaterialPurchase(materialPurchaseService.get(Long.valueOf(purchaseId)));
		}
		
		if(StringUtils.isNotBlank(userId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			materialPurReal.setCreateUser(appUserService.get(Long.valueOf(userId)));
		}
		
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			materialPurReal.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		materialPurReal.setProcessRunId(processRun.getRunId());
		
		MaterialPurRealService materialPurRealService = (MaterialPurRealService)AppUtil.getBean("materialPurRealService");
		materialPurRealService.saveMaterialPurReal(materialPurReal, dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
